package com.wk1un2.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wk1un2.dao.MenusDao;
import com.wk1un2.service.MenusService;

@Service
public class MenusServiceImpl implements MenusService {
	@Autowired
	private MenusDao menusdao;

	@Override
	public List<Map<String, Object>> getAllMenus(Map<String, Object> map) {
		// TODO Auto-generated method stub
				List<Map<String, Object>> module= menusdao.getAllMenus(map);
				Map<String, Object> check=new HashMap<>();
				for (Map<String, Object> m : module) {
					check.put("type", 0);
					check.put("checked", 0);
					m.put("checkArr", check);
				}
				return module;
	}

	@Override
	public Integer count(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return menusdao.count(map);
	}

	@Override
	public Integer add(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return menusdao.add(map);
	}

	@Override
	public Integer update(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return menusdao.update(map);
	}

	
	
	
	@Override
	public Integer delete(Integer menuid) {
		// TODO Auto-generated method stub
		return menusdao.delete(menuid);
	}

	@Override
	public Integer getRolesCount(Integer menuid) {
		// TODO Auto-generated method stub
		return menusdao.getRolesCount(menuid);
	}
	

	
	

	@Override
	public List<Map<String, Object>> getRolesMenus(Integer rm_relationid) {
		List<Map<String, Object>> list = new ArrayList<>();
		list = menusdao.getRolesMenus(rm_relationid);
		List<Map<String, Object>> TreeNodes = new ArrayList<Map<String, Object>>();
		for (Map treeNode : list) {
			if (Integer.parseInt(treeNode.get("menufatherid").toString()) == -1 || treeNode.get("menufatherid") == null) {
				TreeNodes.add(findChildren(treeNode, list));
			}
		}
		// TODO Auto-generated method stub
		return TreeNodes;
	}

	public Map<String, Object> findChildren(Map<String, Object> treeNode, List<Map<String, Object>> treeList) {
		for (Map item : treeList) {
			System.out.println("===>"+item);
			System.out.println("========>"+treeList);
			List c=new ArrayList<Map<String, Object>>();
			if (treeNode.get("menuid").equals(item.get("menufatherid"))) {
				if (treeNode.get("child")==null) {
					treeNode.put("child",c);
					treeNode.put("children",c);
				}
				List ss=(List) treeNode.get("child");
				//treeNode.getChild().add(findChildren(item, treeList));
				ss.add(findChildren(item, treeList));
//				treeNode.put("Child",findChildren(item, treeList));
			}
		}
		return treeNode;
	}

	@Override
	public List<Map<String, Object>> getMenusName(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return menusdao.getMenusName(map);
	}


	

}
